How to implement a viewpager with bottomnavigationview using new navigation architecture component in Android? 您所在的位置:网站首页 tab layout using viewpager2 in android app android How to implement a viewpager with bottomnavigationview using new navigation architecture component in Android?

How to implement a viewpager with bottomnavigationview using new navigation architecture component in Android?

2023-03-29 22:50| 来源: 网络整理| 查看: 265

In modern Android development, the Navigation Architecture Component is commonly used to handle navigation within an app. The combination of a ViewPager and BottomNavigationView can offer a smooth user experience by allowing the user to swipe through different pages while also having a persistent navigation bar at the bottom. In this tutorial, we will be discussing how to implement a ViewPager with BottomNavigationView using the Navigation Architecture Component.

Method 1: Use ViewPager2

To implement a ViewPager with BottomNavigationView using the new Navigation Architecture Component, we can use ViewPager2. Here are the steps to follow:

Add the ViewPager2 and BottomNavigationView to your layout file. Create a new menu resource file for the BottomNavigationView. Create a new ViewPager2 adapter. class ViewPagerAdapter(fragmentManager: FragmentManager, lifecycle: Lifecycle) : FragmentStateAdapter(fragmentManager, lifecycle) { override fun getItemCount(): Int = 3 override fun createFragment(position: Int): Fragment { return when (position) { 0 -> HomeFragment() 1 -> DashboardFragment() 2 -> NotificationsFragment() else -> HomeFragment() } } } Set up the ViewPager2 and BottomNavigationView in your activity or fragment. class MainActivity : AppCompatActivity() { private lateinit var viewPager: ViewPager2 private lateinit var bottomNavigationView: BottomNavigationView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) viewPager = findViewById(R.id.viewPager) bottomNavigationView = findViewById(R.id.bottomNavigationView) val adapter = ViewPagerAdapter(supportFragmentManager, lifecycle) viewPager.adapter = adapter bottomNavigationView.setOnNavigationItemSelectedListener { item -> when (item.itemId) { R.id.menu_home -> viewPager.currentItem = 0 R.id.menu_dashboard -> viewPager.currentItem = 1 R.id.menu_notifications -> viewPager.currentItem = 2 } true } viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() { override fun onPageSelected(position: Int) { super.onPageSelected(position) when (position) { 0 -> bottomNavigationView.menu.findItem(R.id.menu_home).isChecked = true 1 -> bottomNavigationView.menu.findItem(R.id.menu_dashboard).isChecked = true 2 -> bottomNavigationView.menu.findItem(R.id.menu_notifications).isChecked = true } } }) } }

That's it! Now you have a ViewPager with BottomNavigationView using the new Navigation Architecture Component and ViewPager2.

Method 2: Use a FragmentStateAdapter

Here are the steps to implement a ViewPager with BottomNavigationView using new Navigation Architecture Component with FragmentStateAdapter:

Add the Navigation Architecture Component dependency to your project. implementation 'androidx.navigation:navigation-fragment-ktx:2.3.0' implementation 'androidx.navigation:navigation-ui-ktx:2.3.0' Create a new navigation graph file in the res/navigation folder of your project. Create your ViewPager and BottomNavigationView in your activity layout file. Create a menu resource file to define the BottomNavigationView items. Create a FragmentStateAdapter to manage the ViewPager fragments. class ViewPagerAdapter(activity: FragmentActivity) : FragmentStateAdapter(activity) { override fun getItemCount(): Int { return 3 } override fun createFragment(position: Int): Fragment { return when (position) { 0 -> HomeFragment() 1 -> SearchFragment() 2 -> ProfileFragment() else -> HomeFragment() } } } In your activity, set up the ViewPager and BottomNavigationView with the Navigation Architecture Component. class MainActivity : AppCompatActivity() { private lateinit var viewPager: ViewPager2 private lateinit var bottomNavigationView: BottomNavigationView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) viewPager = findViewById(R.id.viewPager) bottomNavigationView = findViewById(R.id.bottomNavigationView) val navController = findNavController(R.id.nav_host_fragment) val appBarConfiguration = AppBarConfiguration( setOf( R.id.homeFragment, R.id.searchFragment, R.id.profileFragment ) ) setupActionBarWithNavController(navController, appBarConfiguration) bottomNavigationView.setupWithNavController(navController) viewPager.adapter = ViewPagerAdapter(this) viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() { override fun onPageSelected(position: Int) { bottomNavigationView.menu.getItem(position).isChecked = true } }) bottomNavigationView.setOnNavigationItemSelectedListener { item -> when (item.itemId) { R.id.homeFragment -> { viewPager.currentItem = 0 true } R.id.searchFragment -> { viewPager.currentItem = 1 true } R.id.profileFragment -> { viewPager.currentItem = 2 true } else -> false } } } }

That's it! You now have a ViewPager with BottomNavigationView using new Navigation Architecture Component with FragmentStateAdapter.

Method 3: Set up Navigation Graph and Destinations

Sure, I can help you with that. Here is a step-by-step guide to implementing a ViewPager with BottomNavigationView using the new Navigation Architecture Component in Android:

First, add the Navigation Architecture Component to your project by adding the following dependencies to your app-level build.gradle file: implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5' implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'

Next, create a new navigation graph file by right-clicking on the res folder in your project and selecting "New > Android Resource File". Give the file a name (e.g. "nav_graph") and select "Navigation" as the resource type.

Open the nav_graph.xml file and add two destinations: one for the ViewPager fragment and one for the BottomNavigationView fragment. Give each destination a unique ID (e.g. "nav_viewpager" and "nav_bottomnav").

In your activity layout file, add a tag for the NavHostFragment and set its app:navGraph attribute to the ID of your navigation graph file. Also, add a ViewPager and a BottomNavigationView to the layout. Create a new NavHostFragment instance in your activity and set it as the default NavController for the BottomNavigationView. val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment val navController = navHostFragment.navController bottom_nav.setupWithNavController(navController) In your ViewPager fragment, create a ViewPager adapter and set it to the ViewPager. Also, create a TabLayout and set it up with the ViewPager. class ViewPagerFragment : Fragment(R.layout.fragment_view_pager) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) val viewPager = view.findViewById(R.id.view_pager) val tabLayout = view.findViewById(R.id.tab_layout) val adapter = ViewPagerAdapter(childFragmentManager) viewPager.adapter = adapter TabLayoutMediator(tabLayout, viewPager) { tab, position -> tab.text = "Tab ${position + 1}" }.attach() } private class ViewPagerAdapter(fragmentManager: FragmentManager) : FragmentStatePagerAdapter(fragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) { override fun getCount(): Int = 3 override fun getItem(position: Int): Fragment { return ViewPagerItemFragment.newInstance(position) } } } Finally, create a new NavDirections instance for each destination in your navigation graph and use them to navigate between the ViewPager and BottomNavigationView fragments. val viewPagerDirections = ViewPagerFragmentDirections.actionNavViewpagerToNavBottomnav() val bottomNavDirections = BottomNavFragmentDirections.actionNavBottomnavToNavViewpager() viewPagerDirections.let { navController.navigate(it) } bottomNavDirections.let { navController.navigate(it) }

That's it! With these steps, you should be able to implement a ViewPager with BottomNavigationView using the new Navigation Architecture Component in Android.

Method 4: Connect BottomNavigationView with NavigationUI

To implement a ViewPager with BottomNavigationView using new Navigation Architecture Component in Android, you can connect BottomNavigationView with NavigationUI. Here are the steps to do it:

Add the Navigation Architecture Component dependencies to your app-level build.gradle file: dependencies { implementation "android.arch.navigation:navigation-fragment-ktx:1.0.0" implementation "android.arch.navigation:navigation-ui-ktx:1.0.0" } In your activity layout file, add a ViewPager and a BottomNavigationView: Create a NavHostFragment in your activity layout file:

Create a navigation graph file (nav_graph.xml) and define the destinations and actions for your app.

In your activity code, find the BottomNavigationView and connect it with the NavController:

val bottomNavigationView = findViewById(R.id.bottom_navigation) val navController = findNavController(R.id.nav_host_fragment) bottomNavigationView.setupWithNavController(navController) In your fragment code, find the ViewPager and connect it with the NavController: val viewPager = view.findViewById(R.id.view_pager) val navController = findNavController() val adapter = ViewPagerAdapter(childFragmentManager, navController) viewPager.adapter = adapter Create a ViewPagerAdapter to manage the ViewPager: class ViewPagerAdapter(fragmentManager: FragmentManager, private val navController: NavController) : FragmentPagerAdapter(fragmentManager) { private val fragments = listOf( R.id.home_fragment, R.id.search_fragment, R.id.profile_fragment ).map { navController.createDestination().apply { id = it } } override fun getItem(position: Int): Fragment { return NavHostFragment.create(fragments[position]).apply { navController = [email protected] } } override fun getCount(): Int { return fragments.size } }

That's it! Now you have a ViewPager with BottomNavigationView using new Navigation Architecture Component in Android.



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有